package com.coai.ai.model

import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
import java.time.LocalDateTime

/**
 * AI对话历史
 */
@Document(collection = "conversation_histories")
data class ConversationHistory(
    @Id
    val id: String? = null,
    
    // 会话ID（关联业务系统）
    val conversationId: String,
    
    // 用户ID
    val userId: String,
    
    // 对话消息列表
    val messages: MutableList<ChatMessage> = mutableListOf(),
    
    // AI模型
    val model: String = "gpt-4",
    
    // 对话上下文
    val context: String? = null,
    
    // 创建时间
    val createdAt: LocalDateTime = LocalDateTime.now(),
    
    // 更新时间
    var updatedAt: LocalDateTime = LocalDateTime.now()
)

/**
 * 聊天消息
 */
data class ChatMessage(
    val role: MessageRole,
    val content: String,
    val timestamp: LocalDateTime = LocalDateTime.now(),
    val metadata: Map<String, Any>? = null
)

enum class MessageRole {
    SYSTEM,    // 系统提示
    USER,      // 用户消息
    ASSISTANT  // AI助手回复
}

/**
 * 客户画像
 */
@Document(collection = "customer_profiles")
data class CustomerProfile(
    @Id
    val id: String? = null,
    
    val customerId: String,
    
    // 基本信息
    val name: String?,
    val company: String?,
    val industry: String?,
    val position: String?,
    
    // 行为数据
    val interactionCount: Int = 0,
    val lastInteractionAt: LocalDateTime? = null,
    val averageResponseTime: Long? = null, // 秒
    
    // 偏好
    val preferences: Map<String, Any> = emptyMap(),
    
    // 标签
    val tags: List<String> = emptyList(),
    
    // 情绪分析历史
    val sentimentHistory: List<SentimentRecord> = emptyList(),
    
    // 购买意向评分 0-100
    val purchaseIntentScore: Double? = null,
    
    // 创建时间
    val createdAt: LocalDateTime = LocalDateTime.now(),
    
    // 更新时间
    var updatedAt: LocalDateTime = LocalDateTime.now()
)

/**
 * 情绪记录
 */
data class SentimentRecord(
    val score: Double, // -1.0 到 1.0
    val text: String,
    val timestamp: LocalDateTime = LocalDateTime.now()
)

/**
 * 知识库条目
 */
@Document(collection = "knowledge_base")
data class KnowledgeEntry(
    @Id
    val id: String? = null,
    
    // 问题
    val question: String,
    
    // 答案
    val answer: String,
    
    // 分类
    val category: String,
    
    // 标签
    val tags: List<String> = emptyList(),
    
    // 向量嵌入（用于语义搜索）
    val embedding: List<Double>? = null,
    
    // 使用次数
    var usageCount: Int = 0,
    
    // 有效性评分
    var effectivenessScore: Double = 1.0,
    
    val createdAt: LocalDateTime = LocalDateTime.now(),
    var updatedAt: LocalDateTime = LocalDateTime.now()
)
